<?php
/**
* @filename source_module.php 2013-11-11 02:10:56x
* @author Huming Xu <huming17@126.com>
* @version 1.0.0
* @copyright DZF (c) 2013, Huming Xu
*/

class ctrl_user_group extends user_group{
    public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
    
    static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
    /*
	*	取出一条信息
	*/
	public function do_detail($ug_id='') {
        global $_G;
		$detail = array();
        $ug_id = isset($_REQUEST['ug_id']) ? $_REQUEST['ug_id'] : $ug_id;
        $detail = $this->one_info('user_group','ug_id',$ug_id);
        if(!empty($ug_id) && !empty($detail)){
            if($detail['create_dateline']){
                $detail['create_dateline'] = date('Y-m-d H:i:s',$detail['create_dateline']);
            }
            if($detail['modify_dateline']){
                $detail['modify_dateline'] = date('Y-m-d H:i:s',$detail['modify_dateline']);
            }
            $ug_master_user_info = $this->one_info('user_detail','user_id',$detail['ug_master_user_id']);
            $detail['ug_master_user_info']['user_realname']=$ug_master_user_info['user_realname'];
            $detail['ug_master_user_info']['user_nickname']=$ug_master_user_info['user_nickname'];
            $detail['ug_master_user_info']['user_avatar']=$ug_master_user_info['user_avatar'];
            $data['user_group_detail'] = $detail;
            if($detail){
                $return['data'] = $data;
                $return['code'] = '0';
            }else{
                $return['code'] = '1';
            }
        }else{
            $return['code'] = '2';
        }
        if($_G['gp_api']){
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            die();
        }else{
            include template('user/group/detail');    
        }
	}
    
	/*
	*  添加系统信息
	*/
	public function do_add() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$user_group = isset($_REQUEST['user_group']) && !empty($_REQUEST['user_group']) ? $_REQUEST['user_group']:'';
		if('1'==$issubmit){
            //TODO 后端字段校验 根据具体业务逻辑添加
            //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
            foreach($user_group AS $key => $value){
                if(is_array($value) && !empty($value)){
                    $user_group[$key] = implode(",", $value);
                }
            }
            $user_group['create_dateline'] = TIMESTAMP;
            $insert_id = $this->add('user_group',$user_group);
            if($insert_id){
                showmessage('操作成功','index.php?mod=user&action=group&do=index');
            }else{
                showmessage('操作失败','index.php?mod=user&action=group&do=index');
            }
		}else{
			include template('user/group/add');
		}
	}
    
	/*
	*  编辑系统信息
	*/
	public function do_edit() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$user_group = isset($_REQUEST['user_group']) && !empty($_REQUEST['user_group']) ? $_REQUEST['user_group']:'';
        $ug_id = isset($_REQUEST['ug_id']) ? $_REQUEST['ug_id'] : '';
        if($ug_id){
            if('1'==$issubmit){
                $user_group['modify_dateline'] = TIMESTAMP;
                //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
                foreach($user_group AS $key => $value){
                    if(is_array($value) && !empty($value)){
                        $user_group[$key] = implode(",", $value);
                    }
                }                
                $where = array('ug_id'=>$ug_id);
                $effect_row = $this->edit('user_group',$user_group,$where);
                if($effect_row){
                    showmessage('操作成功','index.php?mod=user&action=group&do=index');
                }else{
                    showmessage('操作失败','index.php?mod=user&action=group&do=index');
                }
            }else{
                //DEBUG 获取操作对象信息
                $info = $this->one_info('user_group','ug_id',$ug_id);
                include template('user/group/edit');
            }
        }else{
            showmessage('请选择编辑对象','index.php?mod=user&action=group&do=index');
        }
	}
    
	/*
	*  删除系统信息
	*/
	public function do_delete() {
        global $_G;
        $ug_id = isset($_REQUEST['ug_id']) ? $_REQUEST['ug_id'] : '';
        if($ug_id){
            $effect_row = $this->delete("user_group", array("ug_id" => $ug_id), $limit=1);
            if($effect_row){
                showmessage('操作成功','index.php?mod=user&action=group&do=index');
            }else{
                showmessage('操作失败','index.php?mod=user&action=group&do=index');
            }
        }
    }
	/*
	*  系统信息列表
	*/
	public function do_index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if($keyword){
            $wheresql = " AND ug_name LIKE '%".$keyword."%' ";
        }
        $orderby = " ORDER BY ug_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->index('user_group',$page_condition);
        $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=user&action=group&do=index&keyword=".$keyword);
		include template('user/group/index');	
	}

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function do_api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $orderby = " ORDER BY ug_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->api('user_group',$page_condition);      
        if('json'==$return_type){
            $return = json_encode($page_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
		echo $return;
	}
    
    /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function do_groups(){
        global $_G;
        $user_id = isset($_REQUEST['user_id']) && !empty($_REQUEST['user_id']) ? $_REQUEST['user_id'] : $_G['user_id'];
        if($user_id){
            $groups = DB::fetch_all("SELECT ug_id FROM ".DB::table('user_group_member')." WHERE user_id = '".$user_id."' AND isdelete=0");
            foreach($groups AS $key => $value){
                $value['ug_name']=$this->get_field_value_by_id('user_group','ug_id',$value['ug_id'],'ug_name');
                $value['ug_notice']=$this->get_field_value_by_id('user_group','ug_id',$value['ug_id'],'ug_notice');
                $value['ug_master_user_id']=$this->get_field_value_by_id('user_group','ug_id',$value['ug_id'],'ug_master_user_id');
                if($value['ug_master_user_id']){
                   $value['ug_master_user_realname']=ext::user_realname($value['ug_master_user_id']); 
                }
                $value['timestamp']=TIMESTAMP;
                $group_info['user_group_groups'][] = $value;
            }
            if($group_info){
                $return['data'] = $group_info;
                $return['code'] = '0';
            }else{
                $return['code'] = '1';
            } 
        }else{
            $return['code'] = '2';
        }
        if($_G['gp_api']){
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }
	}
}
?>